package com.bishe.demo.modules.other.dao;

import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.entity.Image;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ImageDao {
    @Insert("insert into other_image values(null,#{image})")
    @Options(useGeneratedKeys = true, keyProperty = "imageId", keyColumn = "image_id")
    void insertImage(Image image);

    @Delete("delete from other_image where image_id=#{imageId}")
    void deleteImageByImageId(int imageId);

    @Select("select * from other_image where image_id=#{imageId}")
    Image getImageByImageId(int imageId);

    @Select("select * from other_image oi left join other_scenery_image osi on osi.image_id=oi.image_id where osi.scenery_id=#{sceneryId}")
    List<Image> getImagesBySceneryId(int sceneryId);

    @Select("select * from other_image oi left join other_travel_image oti on oti.image_id=oi.image_id where oti.travel_id=#{travelId}")
    List<Image> getImageByTravelId(int travelId);

    @Select("<script>"
            + "select * from other_image "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (image like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by image_id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Image> getImagesBySearchBean(SearchBean searchBean);
}
